package org.softmed.rest.generation.xml;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class PagedList {

	public static int itemsPerPage = 20;
	public static int maxItemsPerPage = 100;

	List<Object> items = new ArrayList<Object>();
	int currentPageNumber;
	String previousURI;
	String nextURI;
	String firstURI;
	String lastURI;
	long itemCount;

	int currentItemsPerPage;

	int initialIndex;
	int finalIndex;
	long lastPage;

	public PagedList() {
		currentItemsPerPage = itemsPerPage;
	}

	public void setupPage(boolean detailed, long count, int currentPage,
			String baseURI) {
		currentPageNumber = currentPage;

		itemCount = count;

		lastPage = count / currentItemsPerPage;
		if (count % currentItemsPerPage != 0)
			lastPage += 1;

		initialIndex = currentItemsPerPage * (currentPageNumber - 1);
		finalIndex = currentItemsPerPage * currentPageNumber - 1;

		if (count > currentItemsPerPage) {

			if (count > finalIndex + 1) {
				setNextPageURI(baseURI + getCorrectSizeParam()
						+ isDetailed(detailed) + "&page="
						+ (currentPageNumber + 1));
			}

			if (initialIndex > 0) {
				setPreviousPageURI(baseURI + getCorrectSizeParam()
						+ isDetailed(detailed) + "&page="
						+ (currentPageNumber - 1));
				setFirstPageURI(baseURI + getCorrectSizeParam()
						+ isDetailed(detailed) + "&page=1");
			}

			if (currentPageNumber != lastPage)
				setLastPageURI(baseURI + getCorrectSizeParam()
						+ isDetailed(detailed) + "&page=" + lastPage);
		}

	}

	protected String isDetailed(boolean detailed) {
		return (detailed ? "&detailed" : "");
	}

	private String getCorrectSizeParam() {
		return "?size=" + currentItemsPerPage;
	}

	public void setObjects(Collection<?> objects) {
		items.clear();
		if (objects.size() > (currentItemsPerPage + 1))
			throw new RuntimeException("Added more items that the max of "
					+ currentItemsPerPage);

		for (Object object : objects) {
			items.add(object);
		}

	}

	public List<Object> getItems() {
		return items;
	}

	public void setItems(List<Object> items) {
		this.items = items;
	}

	public int getCurrentPageNumber() {
		return currentPageNumber;
	}

	public void setCurrentPageNumber(int pageNumber) {
		this.currentPageNumber = pageNumber;
	}

	public String getPreviousPageURI() {
		return previousURI;
	}

	public void setPreviousPageURI(String previousPageURI) {
		this.previousURI = previousPageURI;
	}

	public String getNextPageURI() {
		return nextURI;
	}

	public void setNextPageURI(String nextPageURI) {
		this.nextURI = nextPageURI;
	}

	public String getFirstPageURI() {
		return firstURI;
	}

	public void setFirstPageURI(String firstPageURI) {
		this.firstURI = firstPageURI;
	}

	public String getLastPageURI() {
		return lastURI;
	}

	public long getLastPage() {
		return lastPage;
	}

	public void setLastPageURI(String lastPageURI) {
		this.lastURI = lastPageURI;
	}

	public static int getItemsPerPage() {
		return itemsPerPage;
	}

	public static void setItemsPerPage(int itemsPerPage) {
		PagedList.itemsPerPage = itemsPerPage;
	}

	public long getItemCount() {
		return itemCount;
	}

	public void setItemCount(int itemCount) {
		this.itemCount = itemCount;
	}

	public int getCurrentItemsPerPage() {
		return currentItemsPerPage;
	}

	public void setCurrentItemsPerPage(int currentItemsPerPage) {
		this.currentItemsPerPage = currentItemsPerPage;
		if (this.currentItemsPerPage > maxItemsPerPage)
			this.currentItemsPerPage = maxItemsPerPage;
	}

	public int getInitialIndex() {
		return initialIndex;
	}

	public int getFinalIndex() {
		return finalIndex;
	}

	public void setLastPage(long lastPage) {
		this.lastPage = lastPage;
	}

}
